package Spring_cloud_firstdemo.cloud_mysql_demo.controllers;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import Spring_cloud_firstdemo.cloud_mysql_demo.models.User;
import Spring_cloud_firstdemo.cloud_mysql_demo.models.UserDao;;


@Controller
public class UserController {
	 MainController m=new MainController();

	  /**
	   * /create  --> Create a new user and save it in the database.
	   * 
	   * @param email User's email
	   * @param name User's name
	   * @return A string describing if the user is succesfully created or not.
	   */
	  @RequestMapping("/create")
	  @ResponseBody
	  public String create(String email, String name) {
	    User user = null;
	    System.out.println("create...");
	    try {
	      user = new User(email, name);
	      userDao.save(user);
	    }
	    catch (Exception ex) {
	    	//返回的必须是json格式的数据，"\字符串  \""不会报错
	      return "\"Error creating the user: " + ex.toString()+"\"";
	    }
	    return "\"User succesfully created!\"" ;
	  }
	  
	  /**
	   * /delete  --> Delete the user having the passed id.
	   * 
	   * @param id The id of the user to delete
	   * @return A string describing if the user is succesfully deleted or not.
	   */
	  @RequestMapping("/delete")
	  @ResponseBody
	  public String delete(long id) {
		
	    try {
	      User user = new User(id);
	      userDao.delete(user);
	    }
	    catch (Exception ex) {
	      return "\"Error deleting the user: " + ex.toString()+"\"";
	    }
	    return "\"User succesfully deleted!\"" ;
	  }
	  
	  /**
	   * /get-by-email  --> Return the id for the user having the passed email.
	   * 
	   * @param email The email to search in the database.
	   * @return The user id or a message error if the user is not found.
	   */
	  @RequestMapping("/get-by-email")
	  @ResponseBody
	  public String getByEmail(String email) {
	    String userId,userEmail, userName;
	    try {
	      User user = userDao.findByEmail(email);
	      userId = String.valueOf(user.getId());
	      userEmail=email;
	      userName = String.valueOf(user.getName());
	    }
	    catch (Exception ex) {
	      return "User not found";
	    }
	    return " Found  id: " + userId + " email: "+userEmail+" name: "+userName;
	  }
	  
	  /**
	   * /update  --> Update the email and the name for the user in the database 
	   * having the passed id.
	   * 
	   * @param id The id for the user to update.
	   * @param email The new email.
	   * @param name The new name.
	   * @return A string describing if the user is succesfully updated or not.
	   */
	  @RequestMapping("/update")
	  @ResponseBody
	  public ArrayList<User> updateUser(long id, String email, String name) {
		
	    try {
	      User user = userDao.findOne(id);
	      user.setEmail(email);
	      user.setName(name);
	      userDao.save(user);
	    }
	    catch (Exception ex) {
	      return null;
	    }
	    
	    return m.service();
	  }

	  // ------------------------
	  // PRIVATE FIELDS
	  // ------------------------

	  @Autowired
	  private UserDao userDao;

}
